package agent_store

import (
	"context"
	"encoding/json"
	"xialou_platform/app/errorx"
	"xialou_platform/app/tools"

	"xialou_platform/services/agent/api/internal/svc"
	"xialou_platform/services/agent/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SaleDetailsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSaleDetailsLogic(ctx context.Context, svcCtx *svc.ServiceContext) SaleDetailsLogic {
	return SaleDetailsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SaleDetailsLogic) SaleDetails(req *types.StoreDetailReq) (resp *types.SaleDetailsResp, err error) {

	reqStr, err := json.Marshal(StoreDetailReq{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}
	bytes, err := tools.RequestOtherPlatform(
		l.svcCtx.Config.XiuXianApi.SaleDetails,
		"POST",
		l.svcCtx.Config.Auth.AccessSecret,
		string(reqStr),
		l.svcCtx.Config.Auth.AccessExpire,
		10000, // admin_users表 下楼系统账户的ID
	)
	if err != nil {
		return nil, err
	}

	var res SaleDetailsResp
	err = json.Unmarshal(bytes, &res)
	if err != nil {
		logx.Infof("调用%v商家销售详情接口err：%v", req.Platform, err.Error())
		return nil, errorx.NewMsgReasonCodeError(errorx.DefaultCode, "调用商家销售详情接口err：%v", err.Error())
	}

	logx.Infof("商家销售详情: %v", res)
	if res.Code != 200 {
		return nil, errorx.NewCodeReason(res.Code, res.Message)
	}

	return &res.Data, nil
}

type StoreDetailReq struct {
	Id string `json:"id"`
}

type SaleDetailsResp struct {
	Code    int                   `json:"code"`
	Message string                `json:"message"`
	Data    types.SaleDetailsResp `json:"data"`
}
